package com.test.thread;

import java.util.HashMap;

/**
 * 死锁\
 * jps  windows下的java运行程序 类似linux下ps查看进程的命令 jps -l
 */
public class DeadLockDemo {
    public static void main(String[] args) {
         String lockA = "lockA";
         String lockB = "lockB";
        new Thread(new HoldLockThread(lockA,lockB)).start();
        new Thread(new HoldLockThread(lockB,lockA)).start();
    }
}

class HoldLockThread implements Runnable{
    private String lockA;
    private String lockB;

    public HoldLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"\t自己持有"+lockA+"\t 尝试获取："+lockB);
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"\t自己持有"+lockB+"\t 尝试获取："+lockA);
            }
        }
    }
}